package com.s2u2m.training.ex_programmer.liskov_substitution.bad;

import com.s2u2m.training.ex_programmer.liskov_substitution.Shape;

import java.security.InvalidParameterException;

/**
 * more exception constraints in implements
 */
public class Rectangle implements Shape {
    @Override
    public Double area(Double length, Double width)
            throws InvalidParameterException, NumberFormatException {
        if (length < 0 || width < 0) {
            throw new InvalidParameterException();
        }

        checkFormat(length);
        checkFormat(width);

        return length * width;
    }

    private void checkFormat(Double value) throws NumberFormatException {
        String s = Double.toString(value);
        int index = s.indexOf('.');
        int length = s.substring(index + 1).length();
        if (length > 2) {
            throw new NumberFormatException();
        }
    }
}
